package edu.cuit.zhuyimeng.framework.common.util;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * 时间工具类
 */
public class TimeUtils {

    private static String formatDateByPattern(Date date, String dateFormat){
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String formatTimeStr = null;
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }

    /**
     * Date转为cron表达式
     * @param date 日期
     * @return cron表达式
     */
    public static String getCron(Date  date){
        String dateFormat="ss mm HH dd MM ? yyyy";
        return formatDateByPattern(date, dateFormat);
    }

    /**
     * LocalDateTime转为Date
     * @param localDateTime LocalDateTime
     * @return Date
     */
    public static Date getDate(LocalDateTime localDateTime) {
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    /**
     * LocalDate转为Date
     * @param localDate LocalDate
     * @return Date
     */
    public static Date getDate(LocalDate localDate) {
        return getDate(localDate.atStartOfDay());
    }

    /**
     * Date转为LocalDateTime
     * @param date Date
     * @return LocalDateTime
     */
    public static LocalDate gerLocalData(Date date) {
        return LocalDate.ofInstant(date.toInstant(),ZoneId.systemDefault());
    }

}
